<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>filereader.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_fs_filereader.js.html">filereader.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A wrapper for the HTML5 FileReader object.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.fs.FileReader&#39;);
<a name="line21"></a>goog.provide(&#39;goog.fs.FileReader.EventType&#39;);
<a name="line22"></a>goog.provide(&#39;goog.fs.FileReader.ReadyState&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line25"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line26"></a>goog.require(&#39;goog.fs.Error&#39;);
<a name="line27"></a>goog.require(&#39;goog.fs.ProgressEvent&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * An object for monitoring the reading of files. This emits ProgressEvents of
<a name="line33"></a> * the types listed in {@link goog.fs.FileReader.EventType}.
<a name="line34"></a> *
<a name="line35"></a> * @constructor
<a name="line36"></a> * @extends {goog.events.EventTarget}
<a name="line37"></a> * @final
<a name="line38"></a> */
<a name="line39"></a>goog.fs.FileReader = function() {
<a name="line40"></a>  goog.fs.FileReader.base(this, &#39;constructor&#39;);
<a name="line41"></a>
<a name="line42"></a>  /**
<a name="line43"></a>   * The underlying FileReader object.
<a name="line44"></a>   *
<a name="line45"></a>   * @type {!FileReader}
<a name="line46"></a>   * @private
<a name="line47"></a>   */
<a name="line48"></a>  this.reader_ = new FileReader();
<a name="line49"></a>
<a name="line50"></a>  this.reader_.onloadstart = goog.bind(this.dispatchProgressEvent_, this);
<a name="line51"></a>  this.reader_.onprogress = goog.bind(this.dispatchProgressEvent_, this);
<a name="line52"></a>  this.reader_.onload = goog.bind(this.dispatchProgressEvent_, this);
<a name="line53"></a>  this.reader_.onabort = goog.bind(this.dispatchProgressEvent_, this);
<a name="line54"></a>  this.reader_.onerror = goog.bind(this.dispatchProgressEvent_, this);
<a name="line55"></a>  this.reader_.onloadend = goog.bind(this.dispatchProgressEvent_, this);
<a name="line56"></a>};
<a name="line57"></a>goog.inherits(goog.fs.FileReader, goog.events.EventTarget);
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Possible states for a FileReader.
<a name="line62"></a> *
<a name="line63"></a> * @enum {number}
<a name="line64"></a> */
<a name="line65"></a>goog.fs.FileReader.ReadyState = {
<a name="line66"></a>  /**
<a name="line67"></a>   * The object has been constructed, but there is no pending read.
<a name="line68"></a>   */
<a name="line69"></a>  INIT: 0,
<a name="line70"></a>  /**
<a name="line71"></a>   * Data is being read.
<a name="line72"></a>   */
<a name="line73"></a>  LOADING: 1,
<a name="line74"></a>  /**
<a name="line75"></a>   * The data has been read from the file, the read was aborted, or an error
<a name="line76"></a>   * occurred.
<a name="line77"></a>   */
<a name="line78"></a>  DONE: 2
<a name="line79"></a>};
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Events emitted by a FileReader.
<a name="line84"></a> *
<a name="line85"></a> * @enum {string}
<a name="line86"></a> */
<a name="line87"></a>goog.fs.FileReader.EventType = {
<a name="line88"></a>  /**
<a name="line89"></a>   * Emitted when the reading begins. readyState will be LOADING.
<a name="line90"></a>   */
<a name="line91"></a>  LOAD_START: &#39;loadstart&#39;,
<a name="line92"></a>  /**
<a name="line93"></a>   * Emitted when progress has been made in reading the file. readyState will be
<a name="line94"></a>   * LOADING.
<a name="line95"></a>   */
<a name="line96"></a>  PROGRESS: &#39;progress&#39;,
<a name="line97"></a>  /**
<a name="line98"></a>   * Emitted when the data has been successfully read. readyState will be
<a name="line99"></a>   * LOADING.
<a name="line100"></a>   */
<a name="line101"></a>  LOAD: &#39;load&#39;,
<a name="line102"></a>  /**
<a name="line103"></a>   * Emitted when the reading has been aborted. readyState will be LOADING.
<a name="line104"></a>   */
<a name="line105"></a>  ABORT: &#39;abort&#39;,
<a name="line106"></a>  /**
<a name="line107"></a>   * Emitted when an error is encountered or the reading has been aborted.
<a name="line108"></a>   * readyState will be LOADING.
<a name="line109"></a>   */
<a name="line110"></a>  ERROR: &#39;error&#39;,
<a name="line111"></a>  /**
<a name="line112"></a>   * Emitted when the reading is finished, whether successfully or not.
<a name="line113"></a>   * readyState will be DONE.
<a name="line114"></a>   */
<a name="line115"></a>  LOAD_END: &#39;loadend&#39;
<a name="line116"></a>};
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>/**
<a name="line120"></a> * Abort the reading of the file.
<a name="line121"></a> */
<a name="line122"></a>goog.fs.FileReader.prototype.abort = function() {
<a name="line123"></a>  try {
<a name="line124"></a>    this.reader_.abort();
<a name="line125"></a>  } catch (e) {
<a name="line126"></a>    throw new goog.fs.Error(e, &#39;aborting read&#39;);
<a name="line127"></a>  }
<a name="line128"></a>};
<a name="line129"></a>
<a name="line130"></a>
<a name="line131"></a>/**
<a name="line132"></a> * @return {goog.fs.FileReader.ReadyState} The current state of the FileReader.
<a name="line133"></a> */
<a name="line134"></a>goog.fs.FileReader.prototype.getReadyState = function() {
<a name="line135"></a>  return /** @type {goog.fs.FileReader.ReadyState} */ (this.reader_.readyState);
<a name="line136"></a>};
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * @return {*} The result of the file read.
<a name="line141"></a> */
<a name="line142"></a>goog.fs.FileReader.prototype.getResult = function() {
<a name="line143"></a>  return this.reader_.result;
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * @return {goog.fs.Error} The error encountered while reading, if any.
<a name="line149"></a> */
<a name="line150"></a>goog.fs.FileReader.prototype.getError = function() {
<a name="line151"></a>  return this.reader_.error &amp;&amp;
<a name="line152"></a>      new goog.fs.Error(this.reader_.error, &#39;reading file&#39;);
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Wrap a progress event emitted by the underlying file reader and re-emit it.
<a name="line158"></a> *
<a name="line159"></a> * @param {!ProgressEvent} event The underlying event.
<a name="line160"></a> * @private
<a name="line161"></a> */
<a name="line162"></a>goog.fs.FileReader.prototype.dispatchProgressEvent_ = function(event) {
<a name="line163"></a>  this.dispatchEvent(new goog.fs.ProgressEvent(event, this));
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/** @override */
<a name="line168"></a>goog.fs.FileReader.prototype.disposeInternal = function() {
<a name="line169"></a>  goog.fs.FileReader.base(this, &#39;disposeInternal&#39;);
<a name="line170"></a>  delete this.reader_;
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Starts reading a blob as a binary string.
<a name="line176"></a> * @param {!Blob} blob The blob to read.
<a name="line177"></a> */
<a name="line178"></a>goog.fs.FileReader.prototype.readAsBinaryString = function(blob) {
<a name="line179"></a>  this.reader_.readAsBinaryString(blob);
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>/**
<a name="line184"></a> * Reads a blob as a binary string.
<a name="line185"></a> * @param {!Blob} blob The blob to read.
<a name="line186"></a> * @return {!goog.async.Deferred} The deferred Blob contents as a binary string.
<a name="line187"></a> *     If an error occurs, the errback is called with a {@link goog.fs.Error}.
<a name="line188"></a> */
<a name="line189"></a>goog.fs.FileReader.readAsBinaryString = function(blob) {
<a name="line190"></a>  var reader = new goog.fs.FileReader();
<a name="line191"></a>  var d = goog.fs.FileReader.createDeferred_(reader);
<a name="line192"></a>  reader.readAsBinaryString(blob);
<a name="line193"></a>  return d;
<a name="line194"></a>};
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * Starts reading a blob as an array buffer.
<a name="line199"></a> * @param {!Blob} blob The blob to read.
<a name="line200"></a> */
<a name="line201"></a>goog.fs.FileReader.prototype.readAsArrayBuffer = function(blob) {
<a name="line202"></a>  this.reader_.readAsArrayBuffer(blob);
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Reads a blob as an array buffer.
<a name="line208"></a> * @param {!Blob} blob The blob to read.
<a name="line209"></a> * @return {!goog.async.Deferred} The deferred Blob contents as an array buffer.
<a name="line210"></a> *     If an error occurs, the errback is called with a {@link goog.fs.Error}.
<a name="line211"></a> */
<a name="line212"></a>goog.fs.FileReader.readAsArrayBuffer = function(blob) {
<a name="line213"></a>  var reader = new goog.fs.FileReader();
<a name="line214"></a>  var d = goog.fs.FileReader.createDeferred_(reader);
<a name="line215"></a>  reader.readAsArrayBuffer(blob);
<a name="line216"></a>  return d;
<a name="line217"></a>};
<a name="line218"></a>
<a name="line219"></a>
<a name="line220"></a>/**
<a name="line221"></a> * Starts reading a blob as text.
<a name="line222"></a> * @param {!Blob} blob The blob to read.
<a name="line223"></a> * @param {string=} opt_encoding The name of the encoding to use.
<a name="line224"></a> */
<a name="line225"></a>goog.fs.FileReader.prototype.readAsText = function(blob, opt_encoding) {
<a name="line226"></a>  this.reader_.readAsText(blob, opt_encoding);
<a name="line227"></a>};
<a name="line228"></a>
<a name="line229"></a>
<a name="line230"></a>/**
<a name="line231"></a> * Reads a blob as text.
<a name="line232"></a> * @param {!Blob} blob The blob to read.
<a name="line233"></a> * @param {string=} opt_encoding The name of the encoding to use.
<a name="line234"></a> * @return {!goog.async.Deferred} The deferred Blob contents as text.
<a name="line235"></a> *     If an error occurs, the errback is called with a {@link goog.fs.Error}.
<a name="line236"></a> */
<a name="line237"></a>goog.fs.FileReader.readAsText = function(blob, opt_encoding) {
<a name="line238"></a>  var reader = new goog.fs.FileReader();
<a name="line239"></a>  var d = goog.fs.FileReader.createDeferred_(reader);
<a name="line240"></a>  reader.readAsText(blob, opt_encoding);
<a name="line241"></a>  return d;
<a name="line242"></a>};
<a name="line243"></a>
<a name="line244"></a>
<a name="line245"></a>/**
<a name="line246"></a> * Starts reading a blob as a data URL.
<a name="line247"></a> * @param {!Blob} blob The blob to read.
<a name="line248"></a> */
<a name="line249"></a>goog.fs.FileReader.prototype.readAsDataUrl = function(blob) {
<a name="line250"></a>  this.reader_.readAsDataURL(blob);
<a name="line251"></a>};
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/**
<a name="line255"></a> * Reads a blob as a data URL.
<a name="line256"></a> * @param {!Blob} blob The blob to read.
<a name="line257"></a> * @return {!goog.async.Deferred} The deferred Blob contents as a data URL.
<a name="line258"></a> *     If an error occurs, the errback is called with a {@link goog.fs.Error}.
<a name="line259"></a> */
<a name="line260"></a>goog.fs.FileReader.readAsDataUrl = function(blob) {
<a name="line261"></a>  var reader = new goog.fs.FileReader();
<a name="line262"></a>  var d = goog.fs.FileReader.createDeferred_(reader);
<a name="line263"></a>  reader.readAsDataUrl(blob);
<a name="line264"></a>  return d;
<a name="line265"></a>};
<a name="line266"></a>
<a name="line267"></a>
<a name="line268"></a>/**
<a name="line269"></a> * Creates a new deferred object for the results of a read method.
<a name="line270"></a> * @param {goog.fs.FileReader} reader The reader to create a deferred for.
<a name="line271"></a> * @return {!goog.async.Deferred} The deferred results.
<a name="line272"></a> * @private
<a name="line273"></a> */
<a name="line274"></a>goog.fs.FileReader.createDeferred_ = function(reader) {
<a name="line275"></a>  var deferred = new goog.async.Deferred();
<a name="line276"></a>  reader.listen(goog.fs.FileReader.EventType.LOAD_END,
<a name="line277"></a>      goog.partial(function(d, r, e) {
<a name="line278"></a>        var result = r.getResult();
<a name="line279"></a>        var error = r.getError();
<a name="line280"></a>        if (result != null &amp;&amp; !error) {
<a name="line281"></a>          d.callback(result);
<a name="line282"></a>        } else {
<a name="line283"></a>          d.errback(error);
<a name="line284"></a>        }
<a name="line285"></a>        r.dispose();
<a name="line286"></a>      }, deferred, reader));
<a name="line287"></a>  return deferred;
<a name="line288"></a>};
</pre>


</body>
</html>
